<?php

defined('BY_JYA') or exit('error');
/*
	重复购
*/
$tabs=array(
	array('op'=>'list','title'=>'活动列表'),
	array('op'=>'edit','title'=>'添加/编辑活动'),
	array('op'=>'log','title'=>'活动记录'),
);
set_html('tabs',$tabs);
$_RQ['op']=$_RQ['op']?:'list';
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['params']=array('status'=>0);
	if($_RQ['status']){
		$return['params']['status']=$_RQ['status'];
		if($_RQ['status']==1){
			$params['stop']=0;
			$params['start_time >']=TIMESTAMP;
		}elseif($_RQ['status']==2){
			$params['stop']=0;
			$params['start_time <']=TIMESTAMP;
			$params['end_time >']=TIMESTAMP;
		}elseif($_RQ['status']==3){
			$params['stop']=0;
			$params['end_time <']=TIMESTAMP;
		}elseif($_RQ['status']==4){
			$params['stop']=1;
		}
	}
	if($_RQ['goods_id']){
		$params['goods_id']=intval($_RQ['goods_id']);
		$return['params']['goods_id']=$_RQ['goods_id'];
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array(intval($_RQ['page'])?:1,10),
	);
	$list=$this->get_list('activity_buyagain',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['goods']=$this->model('goods')->goods_detail($item['goods_id'],array('title','image'));
			if($item['stop']){
				$item['status']='4';
			}elseif($item['start_time']>TIMESTAMP){
				$item['status']='1';
			}elseif($item['end_time']<TIMESTAMP){
				$item['status']='3';
			}else{
				$item['status']='2';
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：已关闭活动无法再恢复，只能重新添加，因为关闭活动后，商品有可能参与了其他活动！！','list'=>array(
			array('name'=>'goods.title','title'=>'商品标题','type'=>'string'),
			array('name'=>'goods.image','title'=>'商品图片','type'=>'image'),
			array('name'=>'start_time','title'=>'开始时间','type'=>'time'),
			array('name'=>'end_time','title'=>'结束时间','type'=>'time'),
			array('name'=>'discount','title'=>'折扣','type'=>'string'),
			array('name'=>'num','title'=>'每人可参与次数','type'=>'string'),
			array('name'=>'status','title'=>'活动状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'createtime','title'=>'活动创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'stop','title'=>'关闭','show'=>array('stop'=>0)),
			array('type'=>'href','op'=>'log','title'=>'活动记录'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>15,'name'=>'title','title'=>'活动商品'),
			array('type'=>'select','width'=>15,'name'=>'status','title'=>'活动状态','options'=>'statuss2'),
			array('type'=>'choose','width'=>18,'action'=>'jy_weishop/goods.get_goods/','name'=>'goods_id','title'=>'选择商品'),
		),
	);
	$return['htmls']=$htmls;
	$return['statuss']=array(
		'0'=>'全部',
		'1'=>'未开始',
		'2'=>'进行中',
		'3'=>'已结束',
		'4'=>'已关闭',
	);
	$return['statuss2']=array(
		array('id'=>0,'title'=>'全部',),
		array('id'=>1,'title'=>'未开始',),
		array('id'=>2,'title'=>'进行中',),
		array('id'=>3,'title'=>'已结束',),
		array('id'=>4,'title'=>'已关闭',),
	);
	exi($return);
	
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!is_numeric($post['discount'])){
			exi('折扣必须是整数或小数','error');
		}
		if($post['discount']<0 || $post['discount']>10){
			exi('折扣不能小于0/大于10','error');
		}
		$data=array(
			'goods_id'=>intval($post['goods_id']),
			'start_time'=>strtotime($post['start_time']),
			'end_time'=>strtotime($post['end_time']),
			'discount'=>$post['discount'],
			'num'=>intval($post['num']),
		);
		$id=intval($post['id']);
		if($id){
			if($this->model('activity')->check_active($data['goods_id'])){
				
			}else{
				$this->model('activity')->create_active($data['goods_id'],$id,'buyagain');
			}
			$this->update('activity_buyagain',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			if($this->model('activity')->check_active($data['goods_id'])){
				exi('商品已存在活动，请勿重复添加！','error');
			}
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			$this->insert('activity_buyagain',$data);
			$id=$this->insertid();
			$this->model('activity')->create_active($data['goods_id'],$id,'buyagain');
		}
		exi('设置成功！','','list');
	}
	$id = intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_activity_buyagain',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['choose'] = 0;
		$item['start_time'] =$item['start_time']?date('Y/m/d H:i',$item['start_time']):'';
		$item['end_time'] =$item['end_time']?date('Y/m/d H:i',$item['end_time']):'';
	}else{
		$item=array();
		$item['choose'] = 1;
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置活动详情',
		'post'=>false,
		'edit'=>array(
			array('type'=>'choose','action'=>'jy_weishop/goods.get_goods/','name'=>'goods_id','title'=>'活动商品ID','show'=>array('choose'=>1)),
			array('name'=>'goods_id','title'=>'活动商品ID','type'=>'string','read'=>true,'show'=>array('choose'=>0),'help'=>'只有新添加活动,才能选择商品ID'),
			array('name'=>'start_time','title'=>'开始时间','type'=>'time'),
			array('name'=>'end_time','title'=>'结束时间','type'=>'time'),
			array('name'=>'discount','title'=>'折扣','type'=>'string','help'=>'例如：填写8.5表示打85折'),
			array('name'=>'num','title'=>'每人可参与次数','type'=>'string'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	model('core/php_ctrl')->del(array(
		'tablename'=>$this->tablename('activity_buyagain'),
	));
}
if($_RQ['op']=='stop'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择活动！','error');
	}
	$item=$this->get('activity_buyagain',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(!$item){
		exi('指定活动不存在~','error');
	}
	$this->model('activity')->del_active($item['goods_id'],$id,'buyagain');
	$this->update('activity_buyagain',array('stop'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('活动关闭成功！','','list');
}
if($_RQ['op']=='log'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['active_id']){
		$params['active_id']=intval($_RQ['active_id']);
	}
	$return['params']=array();
	if($_RQ['goods_id']){
		$params['goods_id']=intval($_RQ['goods_id']);
		$return['params']['goods_id']=$_RQ['goods_id'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array(intval($_RQ['page'])?:1,10),
	);
	$list=$this->get_list('activity_buyagain_log',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['goods']=$this->model('goods')->goods_detail($item['goods_id'],array('title','image'));
			$item['member']=cfc('core')->member_show($item['mid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员','type'=>'member'),
			array('name'=>'goods.title','title'=>'商品标题','type'=>'string'),
			array('name'=>'goods.image','title'=>'商品图片','type'=>'image'),
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'num','title'=>'购买数量','type'=>'string'),
			array('name'=>'price','title'=>'购买单价','type'=>'string'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>18,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
			array('type'=>'choose','width'=>18,'action'=>'jy_weishop/goods.get_goods/','name'=>'goods_id','title'=>'选择商品'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}